using System.Text.RegularExpressions;

namespace CSharpPro1._1106;

public class UserServiceImpl : IUserService
{ 
    private List<User> _users = new List<User>();
    public void ShowMainMenu()
    {
        Console.WriteLine("== 主菜单 ==");
        Console.WriteLine("1. 登录");
        Console.WriteLine("2. 注册");
        Console.WriteLine("3. 退出");
    }

    public void Login()
    {
        Console.Clear();
        Console.WriteLine("== 登录用户 ==");
        Console.WriteLine("请输入用户名：");
        string? username = Console.ReadLine();
        if (String.IsNullOrEmpty(username))
        {
            Console.WriteLine("用户名不能为空！！");
            return;
        }
        
        Console.WriteLine("请输入密码：");
        String password = Console.ReadLine();

        if (String.IsNullOrEmpty(password))
        {
            Console.WriteLine("密码不能为空！！");
            return;
        }
        
        bool isLoginSuccess = false;
        foreach (var user in _users)
        {
            if (user.Username == username && user.Password == password)
            {
                isLoginSuccess = true;
                break;
            }
        }

        if (isLoginSuccess)
        {
            Console.WriteLine("登录成功！欢迎使用该系统！");
            Console.WriteLine("== 系统界面 ==");
        }
        else
        {
            Console.WriteLine("用户名不存在或密码错误！登录失败");
        }
    }

    public void Register()
    {
        Console.Clear();
        String username = GetUserName();
        String password = GetPassword();
        Console.WriteLine("请确认密码：");
        string confirmPassword = Console.ReadLine();
        if (password != confirmPassword)
        {
            Console.WriteLine("密码与确认密码不一样！！");
            return;
        }
        _users.Add(new User(username, password));
        Console.WriteLine("注册成功！");
    }

    public void Start()
    {
            bool isRunning = true;
            
            while (isRunning)
            {
                ShowMainMenu();
                string choice = Console.ReadLine();
                
                switch (choice)
                {
                    case "1":
                        Login();
                        break;
                    case "2":
                        Register();
                        break;
                    case "3":
                        isRunning = false;
                        Console.WriteLine("程序已退出，再见！");
                        break;
                    default:
                        Console.WriteLine("输入错误，请输入1-3之间的数字！");
                        break;
                }
            }
    }

    
    private static String GetPassword()
    {
        while (true)
        {
            Console.WriteLine("请输入密码：");
            String password = Console.ReadLine();
            if (String.IsNullOrEmpty(password))
            {
                Console.WriteLine("密码不能为空！！");
                continue;
            }

            if (password.Length < 6 || password.Length > 20)
            {
                Console.WriteLine("密码长度要在6-20之间!!");
                continue;
            }
            //检查密码是否包含 大写字母，小写字母和数字
            bool hasUpper = Regex.IsMatch(password, @"[A-Z]");
            bool hasLower = Regex.IsMatch(password, @"[a-z]");
            bool hasDigit = Regex.IsMatch(password, @"[0-9]");
            if (!hasUpper || !hasLower || !hasDigit)
            {
                Console.WriteLine("密码必须包含大写字母，小写字母和数字");
                continue;
            }
            return password;
        }
    }
    private  String GetUserName()
    {
        while (true)
        {
            Console.WriteLine("请输入用户名：");
            String username = Console.ReadLine();

            if (String.IsNullOrEmpty(username))
            {
                Console.WriteLine("用户名不能为空！！");
                continue;
            }

            if (username.Length < 3 || username.Length > 15)
            {
                Console.WriteLine("用户名长度要在3-15之间！！");
                continue;
            }
            if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
            {
                Console.WriteLine("用户名只能包含字母、数字和下划线！");
                continue;
            }
            bool isExist = false;
            foreach (var user in _users)
            {
                if (user.Username == username)
                {
                    isExist = true;
                    break;
                }
            }
            if (isExist)
            {
                Console.WriteLine("用户名已被注册！");
                continue;
            }
            return username;
        }
    }
}